// console.log("加载成功");

const http = require("http");
const fs = require("fs");

http.createServer((req, res) =>{

    console.log(req.method);
    console.log(req.url);
    // if(req.url == "/index.html"){
    //     res.setHeader("content-type","text/html;charset=utf-8")
    //     // res.end("请求的是index.html");
    //     const strHtml = fs.readFileSync("./public" + req.url).toString();
    //     res.end(strHtml);
    // }else{
    //     res.end("hello world");
    // } 

    res.setHeader("content-type","text/html;charset=utf-8")
    if(req.url == "/" || req.url == "/favicon.ico"){
        res.end("hello world");
    }else{
        try{
            const strHtml = fs.readFileSync("./public" + req.url).toString();
            res.end(strHtml);
        }
        catch (err){
            res.statusCode = 404;
            res.end("404");
        }
        
    }
}).listen(3333);